package pl.edu.mimuw.irs.rcp.controller.editor.bucket.handler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.CommandStack;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.handlers.HandlerUtil;

import pl.edu.mimuw.irs.core.model.analysis.History;
import pl.edu.mimuw.irs.rcp.controller.command.AbstractPersistenceSelectionHandler;
import pl.edu.mimuw.irs.rcp.controller.editor.bucket.Bucket;
import pl.edu.mimuw.irs.rcp.controller.editor.bucket.BucketEditor;
import pl.edu.mimuw.irs.rcp.controller.editor.bucket.command.RemoveRun;
import pl.edu.mimuw.irs.rcp.persistence.PersistenceManager;

public class BucketRemoveRunHandler extends AbstractPersistenceSelectionHandler {

	// *** Attributes

	// *** Methods
	
	@Override
	public Object execute(ExecutionEvent event, CommandStack stack,
			PersistenceManager manager, Object[] selection) {
		
		/* Get editor */
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
		if (!(editor instanceof BucketEditor)) return null;
		BucketEditor bucketEditor = (BucketEditor) editor;
		Bucket bucket = bucketEditor.getModel();
		
		/* Get selection as History */
		
		for (Object object : selection) {
			if (!(object instanceof History)) continue;
			History history = (History) object;
			
			Command command = new RemoveRun(bucket, history);
			stack.execute(command);
		}
		
		return null;
	}
}
